if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var thisPage = null, user = null, html=[];

    var showPublishPage = function () {
        this.init();
    };

    showPublishPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
          var thisModel = this;
          user = getLocalStorage(STORAGE_MANAGER.user);
          if(!user){ 
            redirect("../account/login.html");
            return;
          }
          var requestData = {
          	index:1,
          	size:10,
          	query:'',
          	search:{},
          	sorting:{}
          };
          $.ajax({
          	type:"POST",
          	data:requestData,
          	url:CONFIG_MANAGER.RequstUrl + "v1/goods/virtual/lists",
          	async:true,
          	beforeSend:function(){ $.showWaiting(); }
          }).done(function(data){
          	data = JSON.parse(data);
          	thisModel.initHtml(data.data);
          }).fail(function(data){
          	$.toast(data.responseJSON.Message);
          }).always(function(){
          	$.closeWaiting();
          });
          
        },
        bindEvent: function () {
          //回退
          $("#back").on("click", function () {
            console.log("aaa");
            back();
          });
          
          $("#main").on("click", "#divamount>span", function(){
            $(this).addClass('chongzhi-hover').siblings().removeClass("chongzhi-hover");
            var tip = JSON.parse( $(this).attr("tip") );
            $("#actualValue").html(tip.pay_value);
          });
          
          //点击下一步
          $("#main").on("click", "#charge_now", function () {
          	var tips = JSON.parse(  $("span.chongzhi-hover").attr('tip') );
          	var postData = {
          		org_no:CONFIG_MANAGER.orgId,
          		member_id:user.id,
          		vgood_id:tips.goodid,
          		vgood_qty:1,
          	};
          	$.ajax({
          		type:"POST",
          		url: CONFIG_MANAGER.RequstUrl + "v1/member/asset/wallet/recharge",
          		async:true,
          		data:postData,
          		headers:{ token:user.token },
          		bbeforeSend:function(){ $.showWaiting(); }
          	}).done(function(data){
        			redirect("../shopcart/payCenter.html?mainorderno=" + data.main_order_no + "&selectRechargeAmount=true");
          	}).fail(function(data){
          		console.log( JSON.stringify( data ));
          	}).always(function(){ $.closeWaiting(); });
            
          });
          
        },
        initHtml:function(data){
					html.push('<div class="chongzhi-banner">');
					html.push('<img src="../../images/chongzhi-banner.jpg" />');
					html.push('</div>');
					html.push('<div class="wlxqnrbg mbtopbottom">');
					html.push('<p class="wlxqnr">');
					html.push('<span><b class="ybzt"></b><i class="iconfont icon-qianbao"></i></span>');
					html.push('<input id="username" type="text" class="dengluyhm" style="width:70%" value='+ user.name +' placeholder="请填写用户名">');
					html.push('</p>');
					html.push('</div>');
					html.push('<div id="divamount" class="chongzhi-con">');
					var i = 0;
					while (data[i]){
						var tips = { pay_value:data[i].pay_value, goodid:data[i].virtualgoods_id };
						if(0 == i){
							html.push('<span tip='+ JSON.stringify(tips) +' class="chongzhi-hover">'+ data[i].value +'元</span>');
						}else{
							html.push('<span tip='+ JSON.stringify(tips) +'>'+ data[i].value +'元</span>');
						}
						i++;
					}			
					//html.push('<span class="chongzhi-zidingyi"><input id="userDefined" type="text" placeholder="自定义" class="mmwb"></span>');
					html.push('</div>');
					html.push('<p class="chongzhi-youhui">优惠价：<span style="font-weight:600">&yen;</span><span  style="font-weight:600" id="actualValue">'+data[0].pay_value+'</span></p>');
					html.push('<p id="charge_now" class="sjqyan">立即充值</p>');			
					$("#main").html( html.join('') );
        }
    };

    thisPage = new showPublishPage();
}
